<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model
        ref="form"
        :model="model"
        :rules="validatorRules"
        slot="detail"
        :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
      >
        <a-row>
          <a-card title="单位信息">
            <a-col :span="8">
              <a-form-model-item label="招标人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zbr">
                <a-input-search placeholder="选择招标人" readOnly v-model="model.zbr" @search="doSelectZbr" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="招标联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zblxr">
                <a-input v-model="model.zblxr" placeholder="请输入招标联系人"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="社会信用代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zbshxydm">
                <a-input v-model="model.zbshxydm" placeholder="请输入社会信用代码"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="单位地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zbdwdz">
                <a-input v-model="model.zbdwdz" placeholder="请输入单位地址"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                label="联系方式"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="zblxfs"
                :rules="[{ validator: validatePhone, trigger: 'blur' }]"
              >
                <a-input v-model="model.zblxfs" placeholder="请输入联系方式"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                label="电子邮箱"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="zbdzyx"
                :rules="[{ validator: validateEmail, trigger: 'blur' }]"
              >
                <a-input v-model="model.zbdzyx" placeholder="请输入电子邮箱"></a-input>
              </a-form-model-item>
            </a-col>
          </a-card>
          <a-card title="项目情况" style="margin-top: 15px">
            <a-col :span="8">
              <a-form-model-item label="项目所在区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmszqy">
                <j-dict-select-tag
                  type="list"
                  v-model="model.xmszqy"
                  dictCode="sys_xz_area"
                  placeholder="请选择项目所在区域"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目行业分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmhyfl">
                <j-category-select :selectable="false" v-model="model.xmhyfl" pcode="A04" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
                <a-input v-model="model.projectName" placeholder="请输入项目名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectCode">
                <a-input v-model="model.projectCode" placeholder="请输入项目编号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="监督单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jddw">
                <a-input-search placeholder="选择监督单位" readOnly v-model="model.jddw" @search="doSelectJddw" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="重点项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zdxm">
                <j-dict-select-tag type="radio" v-model="model.zdxm" dictCode="yn" placeholder="请选择重点项目" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmlxdm">
                <j-dict-select-tag
                  type="list"
                  v-model="model.xmlxdm"
                  dictCode="sys_project_type"
                  placeholder="请选择项目类型"
                />
                <!--<j-category-select v-model="model.xmlxdm" pcode="A05"/>-->
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目业主名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmyzmc">
                <a-input v-model="model.xmyzmc" placeholder="请输入项目业主名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="出资比例" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="czbl">
                <a-input v-model="model.czbl" placeholder="请输入出资比例"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmdz">
                <a-input v-model="model.xmdz" placeholder="请输入项目地址"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="投资主体" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tzzt">
                <j-dict-select-tag
                  type="list"
                  v-model="model.tzzt"
                  dictCode="sys_investment_subject"
                  placeholder="请选择投资主体"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="工程类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gclb">
                <j-dict-select-tag
                  type="list"
                  v-model="model.gclb"
                  dictCode="sys_engineering_type"
                  placeholder="请选择工程类别"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="价款形式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jkxs">
                <j-dict-select-tag
                  type="list"
                  v-model="model.jkxs"
                  dictCode="sys_price_type"
                  placeholder="请选择价款形式"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="投资总额(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tzzew">
                <a-input-number v-model="model.tzzew" placeholder="请输入投资总额(万元)"></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                label="本次招标投资额(万元)"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="bczbtzew"
              >
                <a-input-number v-model="model.bczbtzew" placeholder="请输入本次招标投资额(万元)"></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="资金来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zjly">
                <a-input v-model="model.zjly" placeholder="请输入资金来源"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="计划立项批文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jhlxpwh">
                <a-input v-model="model.jhlxpwh" placeholder="请输入计划立项批文号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="资金到位情况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zjdwqk">
                <a-input v-model="model.zjdwqk" placeholder="请输入资金到位情况"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="审核部门名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shbmmc">
                <a-input v-model="model.shbmmc" placeholder="请输入审核部门名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="审核部门代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shbmdm">
                <a-input v-model="model.shbmdm" placeholder="请输入审核部门代码"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="设计单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjdw">
                <a-input v-model="model.sjdw" placeholder="请输入设计单位"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="建设用地批文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jsydpwh">
                <a-input v-model="model.jsydpwh" placeholder="请输入建设用地批文号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="设计文件批准书号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjwjpzsh">
                <a-input v-model="model.sjwjpzsh" placeholder="请输入设计文件批准书号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="其他专项批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtzxpzwh">
                <a-input v-model="model.qtzxpzwh" placeholder="请输入其他专项批准文号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目规模" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmgm">
                <a-input v-model="model.xmgm" placeholder="请输入项目规模"></a-input>
              </a-form-model-item>
            </a-col>
          </a-card>
          <a-card title="招标情况" style="margin-top: 15px">
            <a-col :span="8">
              <a-form-model-item label="项目批文名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmpwmc">
                <a-input v-model="model.xmpwmc" placeholder="请输入项目批文名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="是否固定投资项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sfgdtzxm">
                <a-radio-group v-model="model.sfgdtzxm">
                  <a-radio :value="1">是</a-radio>
                  <a-radio :value="0">否</a-radio>
                </a-radio-group>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                v-if="model.sfgdtzxm == 1"
                label="投资项目统一代码"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="tzxmtydm"
              >
                <a-input v-model="model.tzxmtydm" placeholder="请输入投资项目统一代码"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                v-if="model.sfgdtzxm == 1"
                label="投资项目名称"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="tzxmmc"
              >
                <a-input v-model="model.tzxmmc" placeholder="请输入投资项目名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目批准单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmpzdw">
                <a-input v-model="model.xmpzdw" placeholder="请输入项目批准单位"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="备案时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="basj">
                <j-date placeholder="请选择备案时间" v-model="model.basj" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="受理时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slsj">
                <j-date placeholder="请选择受理时间" v-model="model.slsj" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="招标方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zbfs">
                <a-radio-group v-model="model.zbfs" placeholder="请输入招标方式">
                  <a-row>
                    <a-radio value="1">公开</a-radio>
                    <a-radio value="0">邀请</a-radio>
                  </a-row>
                </a-radio-group>
                <!--                <a-input v-model="model.zbfs" placeholder="请输入招标方式(1公开招标)"></a-input>-->
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmpzwh">
                <a-input v-model="model.xmpzwh" placeholder="请输入项目批准文号"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="招标组织方" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zbzzf">
                <j-dict-select-tag
                  type="list"
                  v-model="model.zbzzf"
                  dictCode="sys_zbzzf_type"
                  placeholder="请选择招标组织方"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="代理机构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dljg">
                <a-input v-model="model.dljg" placeholder="请输入代理机构"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="代理机构信用代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dljgxydm">
                <a-input v-model="model.dljgxydm" placeholder="请输入代理机构信用代码"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="代理机构负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dljgfzr">
                <a-input v-model="model.dljgfzr" placeholder="请输入代理机构负责人"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="代理联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dllxr">
                <a-input v-model="model.dllxr" placeholder="请输入代理联系人"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="代理联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dllxdh">
                <a-input v-model="model.dllxdh" placeholder="请输入代理联系电话"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item
                label="招标范围"
                :labelCol="{ lg: { span: 3 }, sm: { span: 4 } }"
                :wrapperCol="{ lg: { span: 21 }, sm: { span: 20 } }"
                prop="zbfw"
              >
                <a-textarea v-model="model.zbfw" rows="4" placeholder="请输入招标范围" />
              </a-form-model-item>
            </a-col>
            <!--
            <a-col :span="8">
              <a-form-model-item label="招标项目备案表" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zbxmbab">
                <a-input v-model="model.zbxmbab" placeholder="请输入招标项目备案表"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                label="保证金代收代退委托函"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="bzjdsdtwth"
              >
                <a-input v-model="model.bzjdsdtwth" placeholder="请输入保证金代收代退委托函"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                label="开封市公共资源交易领域'八严禁'承诺书"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="kfsbyjcns"
              >
                <a-input v-model="model.kfsbyjcns" placeholder="请输入开封市公共资源交易领域'八严禁'承诺书"></a-input>
              </a-form-model-item>
            </a-col>
            -->
            <a-col :span="24">
              <a-form-model-item
                label="备注"
                :labelCol="{ lg: { span: 3 }, sm: { span: 4 } }"
                :wrapperCol="{ lg: { span: 21 }, sm: { span: 20 } }"
                prop="zbbz"
              >
                <a-textarea v-model="model.zbbz" rows="4" placeholder="请输入备注" />
              </a-form-model-item>
            </a-col>
            <!-- <a-col :span="8">
              <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                <j-upload :projectUploadFlag="true" v-model="model.attachment"></j-upload>
              </a-form-model-item>
            </a-col> -->
          </a-card>
        </a-row>
      </a-form-model>
    </j-form-container>
    <a-card title="标段信息" style="margin-top: 15px" :body-style="{ padding: '2px' }">
      <gc-project-entry-section-list
        ref="gcProjectEntrySectionList"
        :entryId="model.id"
        :disabled="formDisabled"
        @ok="sectionListOk"
      ></gc-project-entry-section-list>
    </a-card>
    <a-card title="资料项" style="margin-top: 15px">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="gcProjectAttachmentTable.columns"
        :dataSource="gcProjectAttachmentTable.dataSource"
        :pagination="false"
        :loading="gcProjectAttachmentTable.loading"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <j-upload file-type="image" v-model="record.fileUrl"></j-upload>
          </template>
        </span>
      </a-table>
    </a-card>
    <a-card style="margin-top: 15px" title="其他附件">
      <a-row>
        <a-col :span="1"></a-col>
        <a-col :span="23">
          <a-form-model-item
            label=""
            :labelCol="{ lg: { span: 3 }, sm: { span: 7 } }"
            :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
            prop="attachment"
          >
            <j-upload :projectUploadFlag="true" v-model="model.attachment"></j-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-card>
    <org-select-list-modal ref="orgSelectListModal" @ok="orgSelectOk" />
    <person-select-list-modal ref="personSelectListModal" @ok="personSelectOk" />
    <div style="margin-top: 15px">
      <ex-operation-record ref="exOperationRecord" :module="'gcProjectEntry'" :entryId="model.id"></ex-operation-record>
    </div>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { getRefPromise, VALIDATE_FAILED } from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import OrgSelectListModal from '@/views/gcOrg/SelectList'
import PersonSelectListModal from '@/views/gcPerson/SelectList'
import GcProjectEntrySectionList from '@/views/gcProject/entrySection/GcProjectEntrySectionList'
import ExOperationRecord from '@/views/exOperationRecord/Index'
import JCategorySelect from '@comp/jeecg/JCategorySelect'
import axios from 'axios'
export default {
  name: 'GcProjectEntryForm',
  mixins: [JVxeTableModelMixin],
  components: {
    JFormContainer,
    OrgSelectListModal,
    PersonSelectListModal,
    GcProjectEntrySectionList,
    ExOperationRecord,
    JCategorySelect,
  },
  data() {
    return {
      toolbarConfig: {
        // add 新增按钮；remove 删除按钮；clearSelection 清空选择按钮
        btn: [],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 9 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      model: { zbr: '', jddw: '' },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 2,
      validatorRules: {
        zbr: [{ required: true, message: '请输入招标人!' }],
        projectName: [{ required: true, message: '请输入项目名称!' }],
        projectCode: [{ required: true, message: '请输入项目编号!' }],
        zbzzf: [{ required: true, message: '请选择招标组织方!' }],
        xmhyfl: [{ required: true, message: '请选择项目行业分类!' }],
        zbdzyx: [{ required: true, message: '请输入电子邮箱!' }],
        xmszqy: [{ required: true, message: '请选择项目所在区域!' }],
        zbdzyx: [{ required: true, message: '请输入电子邮箱!' }],
        jddw: [{ required: true, message: '请选择监督单位!' }],
        xmlxdm: [{ required: true, message: '请选择项目类型代码!' }],
        jkxs: [{ required: true, message: '请选择价款形式!' }],
        xmdz: [{ required: true, message: '请输入项目地址!' }],
        tzzt: [{ required: true, message: '请选择投资主体!' }],
        zbdzyx: [{ required: true, message: '请输入电子邮箱!' }],
        bczbtzew: [{ required: true, message: '请输入本次投资总额!' }],
        xmpwmc: [{ required: true, message: '请输入项目批文名称!' }],
        tzxmtydm: [{ required: true, message: '请输入投资项目统一代码' }],
        xmpzdw: [{ required: true, message: '请输入项目批准单位' }],
        zbfs: [{ required: true, message: '请选择招标方式' }],
        xmpzwh: [{ required: true, message: '请输入项目批准文号' }],
        dljg: [{ required: true, message: '请输入代理机构' }],
      },
      refKeys: ['gcProjectAttachment'],
      tableKeys: ['gcProjectAttachment'],
      activeKey: 'gcProjectAttachment',
      // 资料项
      gcProjectAttachmentTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            },
          },
          {
            title: '模板名称',
            dataIndex: 'templefileName',
            align: 'center',
            width: '300px',
          },
          {
            title: '附件',
            dataIndex: 'fileUrl',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            width: '600px',
          },
          {
            title: '',
          },
        ],
      },
      url: {
        add: '/gcProject/gcProjectEntry/add',
        edit: '/gcProject/gcProjectEntry/edit',
        queryById: '/gcProject/gcProjectEntry/queryById',
        gcProjectAttachment: {
          list: '/gcProject/gcProjectEntry/queryGcProjectAttachmentByMainId',
        },
      },
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    this.fetchData()
  },
  methods: {
    addBefore() {
      this.gcProjectAttachmentTable.dataSource = [
        { templefileName: '招标项目备案表', fileUrl: '' },
        { templefileName: '保证金代收待退委托函', fileUrl: '' },
        { templefileName: '八严禁承诺书', fileUrl: '' },
      ]
    },
    validateEmail(rule, value, callback) {
      // 电子邮箱的正则表达式
      const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
      if (value && !emailRegex.test(value)) {
        callback(new Error('邮箱格式不正确!'))
      } else {
        callback()
      }
    },
    validatePhone(rule, value, callback) {
      // 手机号的正则表达式
      const phoneRegex = /^1[3-9]\d{9}$|^\d{3,4}-\d{7,8}$/
      if (value && !phoneRegex.test(value)) {
        callback(new Error('手机号格式不正确!'))
      } else {
        callback()
      }
    },

    getAllTable() {
      let values = this.tableKeys.map((key) => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      this.$nextTick(() => {})
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.gcProjectAttachment.list, params, this.gcProjectAttachmentTable)
      }
    },

    /** 查询某个tab的数据 */
    requestSubTableData(url, params, tab, success) {
      tab.loading = true
      getAction(url, params)
        .then((res) => {
          let { result } = res
          let dataSource = []
          if (result) {
            if (Array.isArray(result)) {
              dataSource = result
            } else if (Array.isArray(result.records)) {
              dataSource = result.records
            }
          }
          tab.dataSource = dataSource
          typeof success === 'function' ? success(res) : ''
        })
        .finally(() => {
          tab.loading = false
        })
    },

    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        gcProjectAttachmentList: allValues.tablesValue[0].tableData,
      }
    },

    /** 确定按钮点击事件 */
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (!that.gcProjectAttachmentTable.dataSource.every((item) => item.fileUrl)) {
            that.confirmLoading = false
            return that.$message.warning('请检查资料项附件')
          }
          that.model.gcProjectAttachmentList = that.gcProjectAttachmentTable.dataSource
          this.model.gcProjectEntrySectionList = this.$refs.gcProjectEntrySectionList.dataSource
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })

      // /** 触发表单验证 */
      // this.getAllTable().then(tables => {
      //   /** 一次性验证主表和所有的次表 */
      //   return validateFormModelAndTables(this.$refs.form,this.model, tables)
      // }).then(allValues => {
      //   /** 一次性验证一对一的所有子表 */
      //   return this.validateSubForm(allValues)
      // }).then(allValues => {
      //   if (typeof this.classifyIntoFormData !== 'function') {
      //     throw this.throwNotFunction('classifyIntoFormData')
      //   }
      //   let formData = this.classifyIntoFormData(allValues)
      //   // 发起请求
      //   return this.request(formData)
      // }).catch(e => {
      //   if (e.error === VALIDATE_FAILED) {
      //     // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
      //     this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
      //   } else {
      //     console.error(e)
      //   }
      // })
    },

    validateError(msg) {
      this.$message.error(msg)
    },
    doSelectJddw() {
      this.$refs.orgSelectListModal.show([this.projectCode])
    },
    orgSelectOk(obj) {
      this.model.jddwId = obj.id
      this.model.jddw = obj.name
      this.$refs.form.validate()
    },
    doSelectZbr() {
      this.$refs.personSelectListModal.show([this.projectCode])
    },
    personSelectOk(obj) {
      this.model.zbr = obj.orgName
      this.model.zblxr = obj.name
      this.model.zblxfs = obj.telephone
      this.model.zbdwdz = obj.orgAddress
      this.model.zbshxydm = obj.idCard
      this.$refs.form.validate()
    },
    /*
       fetchData() {
           const id = '1874409853827239938';
           // 使用模板字符串正确插入变量
           const url = `ggzyjypt/register/agencyLegal/queryById?id=${id}`;

           fetch(url, {
             method: 'GET',
           })
           .then((response) => {
             if (!response.ok) {
               throw new Error('请求失败');
             }
             return response.json();
           })
           .then((data) => {
             if (data.success) {
               this.model.dljg = data.result.agencyName;
               this.model.dljgxydm = data.result.legalCode;
             } else {
               this.$message.error(data.message || '请求失败');
             }
           })
           .catch((error) => {
             console.error('请求失败:', error);
             this.$message.error('请求失败');
           });
         },
         */
    fetchData() {
      const url = '/register/agencyLegal/queryByUserId'
      const token = localStorage.getItem('token')
      httpAction(url, {}, 'POST', {
        headers: {
          Authorization: `Bearer ${token}`, // 添加 Token 到请求头
        },
      })
        .then((res) => {
          if (res.success) {
            if (res.result) {
              this.model.dljg = res.result.agencyName
              this.model.dljgxydm = res.result.legalCode
              this.model.dljgfzr = res.result.agencyRepresent
              // 根据需要处理其他字段
            }
          } else {
            this.$message.error(res.message || '请求失败')
          }
        })
        .catch((error) => {
          console.error('请求失败:', error)
          this.$message.error('请求失败')
        })
    },

    sectionListOk() {
      this.model.gcProjectEntrySectionList = this.$refs.gcProjectEntrySectionList.dataSource
    },
  },
}
</script>

<style scoped></style>
